package com.jinshan.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinshan.edu.entity.EduTeacher;
import com.jinshan.edu.service.EduTeacherService;
import com.jinshan.exception.EduException;
import com.jinshan.request.QueryTeacherCondition;
import com.jinshan.response.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Jinshan
 * @since 2020-06-21
 */
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有老师
     * @return
     * @throws EduException
     */
    @GetMapping
    public RetVal getAllTeachers() throws EduException{
        try {
            List<EduTeacher> teacherList = eduTeacherService.list(null);
            return RetVal.success().data("teacherList", teacherList);
        } catch (Exception e) {
            throw new EduException(500, "除零异常");
        }
    }

    @GetMapping("/{id}")
    public RetVal queryTeacherById(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        if (teacher != null) {
            return RetVal.success().data("teacher", teacher);
        } else {
            return RetVal.error().message("没有查到id=" + id + "的用户");
        }
    }

    /**
     * 根据id查询老师
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public RetVal deleteTeacherById(@PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return RetVal.success().message("删除成功");
        } else {
            return RetVal.error().message("删除失败");
        }
    }

/*    @GetMapping("/{pageNum}/{pageSize}")
    public RetVal queryTeacherByPage(@PathVariable("pageNum")Integer pageNum,
                                     @PathVariable("pageSize")Integer pageSize) {
        Page<EduTeacher> teacherPage = new Page<>(pageNum, pageSize);
        eduTeacherService.page(teacherPage, null);
        List<EduTeacher> teacherList = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return RetVal.success().data("total", total).data("teacherList", teacherList);
    }*/

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public RetVal queryTeacherByCondition(@PathVariable("pageNum")Integer pageNum,
                                     @PathVariable("pageSize")Integer pageSize,
                                     QueryTeacherCondition queryTeacherCondition) {
        Page<EduTeacher> teacherPage = new Page<>(pageNum, pageSize);
        eduTeacherService.queryTeacherByCondition(teacherPage, queryTeacherCondition);

        List<EduTeacher> teacherList = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return RetVal.success().data("total", total).data("teacherList", teacherList);

    }

    /**
     * 根据前端传过来的参数添加老师
     * @param eduTeacher
     * @return
     */
    @PostMapping
    public RetVal saveTeacher(EduTeacher eduTeacher) {
        System.out.println("*************************************");
        System.out.println(eduTeacher);
        System.out.println("*************************************");
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag) {
            return RetVal.success().message("添加成功");
        } else {
            return RetVal.error().message("添加失败");
        }
    }

    /**
     * 修改老师信息
     * @param eduTeacher
     * @return
     */
    @PutMapping
    public RetVal updateTeacher(EduTeacher eduTeacher) {
        System.out.println("*************************************");
        System.out.println(eduTeacher);
        System.out.println("*************************************");
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return RetVal.success().message("修改成功");
        } else {
            return RetVal.error().message("修改失败");
        }
    }
}

